﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Globalization;

namespace MulticastTV
{
    public class ChannelSetting
    {

        private int _channelNumber;
        private string _channelName;

        private string _aspectRatio;
        private string _cropGeometry; 
        private float _maskRatio;    
        private string _mode;         // for future use (Directshow)
        private int _subtitlePID;
        private int _audioPID;
        private int _videoPID;
        private int _volume;
        private float _zoom;
        private bool _overscan;
        private bool _deinterlace;
        private int _vertOffset;   

        public ChannelSetting()
        {
            Clear();
        }

        public int ChannelNumber
        {
            get { return _channelNumber; }
            set { _channelNumber = value; }
        }

        public string ChannelName
        {
            get { return _channelName; }
            set { _channelName = value; }        
        }
        
        public string AspectRatio
        {
            get { return _aspectRatio;}
            set { _aspectRatio = value;}
        }

        public string CropGeometry
        {
            get { return _cropGeometry; }
            set { _cropGeometry = value; }
        }
        public float MaskRatio
        {
            get { return _maskRatio; }
            set { _maskRatio = value;}
        }

        public string Mode
        {
            get { return _mode; }
            set { _mode = value; }
        }

        public int SubtitlePid
        {
            get { return _subtitlePID; }
            set { _subtitlePID = value; }
        }
        public int AudioPid
        {
            get { return _audioPID; }
            set { _audioPID = value; }
        }
        public int VideoPid
        {
            get { return _videoPID; }
            set { _videoPID = value; }
        }
        public int Volume
        {
            get { return _volume; }
            set { _volume = value; }        
        }

        public float Zoom
        {
            get { return _zoom; }
            set { _zoom = value; }
        }

        public bool Overscan
        {
            get { return _overscan; }
            set { _overscan = value; }
        }

        public bool Deinterlace
        {
            get { return _deinterlace; }
            set { _deinterlace = value; }        
        }

        public int VertOffset           
        {
            get { return _vertOffset; }
            set { _vertOffset = value; }
        }
 
        public void Clear()
        { 
             _aspectRatio="";
             _cropGeometry = ""; 
             _maskRatio = 1.0f;    
             _mode = "";        
             _subtitlePID = -1;
             _audioPID = -1;
             _videoPID = -1;
             _volume = -1;
             _zoom = -1.0f;
             _overscan=false;
             _deinterlace = true;
             _vertOffset = 0;

        }
    }

    /// <summary>
    /// Needed for find method it works only on >3.0
    /// </summary>
    //class ChannelSettingEquality : IEqualityComparer<ChannelSetting>
    //{
    //    public bool Equals(ChannelSetting itema, ChannelSetting itemb)
    //    {
    //        if (itema == null && itemb == null)
    //            return false;

    //        int t1 = itema.ChannelNumber;
    //        string s1 = itema.ChannelName;
    //        int t2 = itemb.ChannelNumber;
    //        string s2 = itemb.ChannelName;
    //        if (t1 == t2 && s1 == s2)
    //            return true;
    //        else
    //            return false;
    //    }

    //    public int GetHashCode(ChannelSetting item)
    //    {
    //        string h = item.ChannelNumber.ToString() + item.ChannelName;
    //        return h.GetHashCode();
    //    }
    //}


    public static class ChannelSettings 
    {
       private static string file = Globals.UserAppPath + @"\..\ChanSettingsV2.txt";
       private static List<ChannelSetting> _list = new List<ChannelSetting>();
       private static CultureInfo provider = CultureInfo.InvariantCulture;

       public static List<ChannelSetting> List
       {
           get {return _list;}
           set {_list = value;}
       }

       public static  Predicate<ChannelSetting> byNameAndNumber (int chn, string name)
       {
           return delegate(ChannelSetting cset)
           {
               return cset.ChannelNumber == chn && cset.ChannelName == name;
           };
       }

       public static Predicate<ChannelSetting> byNumber(int chn)
       {
           return delegate(ChannelSetting cset)
           {
               return cset.ChannelNumber == chn;
           };
       }

       public static void loadFromFile()
       {
           if (File.Exists(file))
           {
               StreamReader sw = File.OpenText(file);
               string line = null; ;
               line = sw.ReadLine();
               while (line != null)
               {
                   string[] d = line.Split(";".ToCharArray());
                   if (d.Length == 14)
                   {
                       string schanNum = d[0];
                       int chanNum = 0;
                       Int32.TryParse(schanNum,out chanNum);
                       string chanName = d[1];
                       string aspectRatio = d[2];
                       string cropGeometry = d[3];
                       string strmaskRatio = d[4];
                       float maskRatio = 1.0f;
                       float.TryParse(strmaskRatio, NumberStyles.Float, provider, out maskRatio);
                       string mode = d[5];
                       string ssubPid = d[6];
                       int subPid =-1;
                       Int32.TryParse(ssubPid, out subPid);
                       string saudPid = d[7];
                       int  audPid = -1;
                       Int32.TryParse(saudPid, out audPid);
                       string svidPid = d[8];
                       int vidPid = -1;
                       Int32.TryParse(svidPid,out vidPid);
                       string svol = d[9];
                       int vol=-1;
                       Int32.TryParse(svol, out vol);
                       string szoom = d[10];
                       float zoom = -1.0f;
                       float.TryParse(szoom, NumberStyles.Float, provider, out zoom);
                       string soverscan = d[11];
                       bool overscan = false;
                       bool.TryParse(soverscan, out overscan);
                       string sdeinterlace = d[12];
                       bool deinterlace = false;
                       bool.TryParse(sdeinterlace, out deinterlace);
                       string strVertOffset = d[13];
                       int vertOffset = 0;
                       Int32.TryParse(strVertOffset, out vertOffset);

                       ChannelSetting set = new ChannelSetting();
                       set.AspectRatio= aspectRatio;
                       set.AudioPid = audPid;
                       set.ChannelName = chanName;
                       set.ChannelNumber = chanNum;
                       set.CropGeometry = cropGeometry;
                       set.Mode = mode;
                       set.SubtitlePid = subPid;
                       set.VideoPid = vidPid;
                       set.Volume =vol;
                       set.Zoom = zoom;
                       set.Overscan = overscan;
                       set.Deinterlace = deinterlace;
                       set.MaskRatio = maskRatio;
                       set.VertOffset = vertOffset;
                       _list.Add(set);
                   }
                       
                   line = sw.ReadLine();
               }
               sw.Close();
           }
       }

       public static void saveToFile()
       {
           StringBuilder sb = new StringBuilder();
           StreamWriter sw = File.CreateText(file);
           foreach (ChannelSetting set in ChannelSettings._list)
           {
               sb.Append(
                   set.ChannelNumber.ToString()+";"+
                   set.ChannelName +";"+
                   set.AspectRatio + ";"+
                   set.CropGeometry + ";"+
                   set.MaskRatio.ToString(provider) + ";"+  
                   set.Mode + ";"+
                   set.SubtitlePid.ToString() + ";"+
                   set.AudioPid.ToString() + ";"+
                   set.VideoPid.ToString() + ";"+
                   set.Volume.ToString() + ";"+
                   set.Zoom.ToString(provider) + ";" +
                   set.Overscan.ToString(provider) + ";"+
                   set.Deinterlace.ToString(provider)+";"+
                   set.VertOffset.ToString()
                   );
               sb.Append(System.Environment.NewLine);
           }
           sw.Write(sb.ToString());
           sw.Close();
       }
    }
 }

